﻿--[[
   @brief    WGBanner
   @author   harleyxwang
   @version  1.0
   @date     2017-04-21
--]]


base_utility.log("loading banner.lua");

ierd_tgp.tpf_ui.Require("wegame_banner\\game_action.lua");
ierd_tgp.tpf_ui.Require('popup_wnd\\control.lua');
ierd_tgp.tpf_ui.Require("wegame_banner\\gametipsmgr.lua");
ierd_tgp.tpf_ui.Require("wegame_banner\\gametips.lua");
ierd_tgp.tpf_ui.Require("wegame_banner\\gametips_expand.lua");

WGTextColor = 
{
    NORMAL = "RGB:255,255,255";
    HIGHLIGHT = "RGB:255,200,0";
    HIGHLIGHT_OVERSEA = "RGB:25,178,255";
    DISABLED = "RGB:119,119,119";
    ERROR = "RGB:246,73,73";
    ALPHA_NORMAL = "255";
    ALPHA_HOVER = "255";
};

WGBanner = {
    BANNER_WIDTH = 190;
    TIPS_HEIGHT = 25;  -- 实际高度25，但是CntHighLight上面有条细条高亮区
    BANNER_NOR_HEIGHT = 45;
    BANNER_WRAPPER_HEIGHT = 45;
    MAX_DISPALY_CHAR_COUNT = 7;
    MAX_EN_DISPALY_CHAR_COUNT = 12;
};

WGBannerType = {
    BANNER_TYPE_NORMAL = 0;
    BANNER_TYPE_SUBSCRIPTION = 1;
};

WGBannerStateIcon = {
    UPDATE_ICON = "ONEIMAGE:banner_list\\update.png;Stretch;null;";
    PURCHASE_ICON = "ONEIMAGE:banner_list\\buy.png;Stretch;null;";
    DOWNLOAD_ICON = "ONEIMAGE:banner_list\\download.png;Stretch;null;";
    SUBSCRIBED_ICON = "ONEIMAGE:banner_list\\wait.png;Stretch;null;";
    RELEASE_ICON = "ONEIMAGE:banner_list\\open_service.png;Stretch;null;";
};

WGBannerUIEventListener = {
};

function WGBanner:new(game_id, wrapper_id, banner_type)
    base_utility.log(string.format("WGBanner:new %d %d", game_id, wrapper_id));
    local o = { 
        game_id_ = game_id,
        wrapper_id_ = wrapper_id,
        select_game_id_ = game_id,
        type_ = banner_type or WGBannerType.BANNER_TYPE_NORMAL;
    };
    setmetatable(o, self)
    self.__index = self
    o:init()
    return o
end

function WGBanner:init()
    self:log("WGBanner:init");
    self.init_ = false;
    self.move_to_game_list_ = false;
    self.text_highlight_color_ = WGTextColor.HIGHLIGHT;
    if base_utility.is_type_of_oversea_ver() then
        self.text_highlight_color_ = WGTextColor.HIGHLIGHT_OVERSEA;
    end
    ------------ 游戏banner ------------- 
    self.banner_normal_id_ = ui_utility.get_ctrl_id_by_path(
            self.wrapper_id_, "Banner_Normal");  
                                  
    self.banner_normal_ptr_ = ui_utility.get_ctrl_ptr_by_id(self.banner_normal_id_);
   
    self:init_nor_banner();

    self.selected_ = false;
    self.in_hover_ = false;
    self.preupdate_status_show_ = false;

    self.mouse_leave_tips_to_banner_ = false;  -- 鼠标是否从tips离开进入banner区域
    self.tips_red_point_can_show_ = FALSE;

    self.cur_banner_state_ = 0;
    self.cur_banner_sub_state_ = 0;
    self.plugin_status = 0;
    self.init_ = true;

    self.game_name = nil;

    local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
    self.is_english_ver = tpfui:Is_English_ver();

end

-- 普通态
function WGBanner:init_nor_banner()
    --self:log("init_nor_banner");

    self.game_info_id_ = ui_utility.get_ctrl_id_by_path(
        self.banner_normal_id_, "CtnGameInfo");
    self.game_info_ptr_ = ui_utility.get_ctrl_ptr_by_path(
        self.banner_normal_id_, "CtnGameInfo");
    -- 游戏名
    self.game_text_id_ = ui_utility.get_ctrl_id_by_path(
        self.banner_normal_id_, "CtnGameInfo\\TxtGameText");
    self.txt_game_text_ptr_ = ui_utility.get_ctrl_ptr_by_path(
        self.banner_normal_id_, "CtnGameInfo\\TxtGameText");
    self.game_text_tip_id_ = ui_utility.get_ctrl_id_by_path(
        self.banner_normal_id_, "BtnBigIcon");
    self.text_color_ = WGTextColor.NORMAL;
    self.text_alpha_ = WGTextColor.ALPHA_NORMAL;
    -- 游戏头像
    self.game_head_id_ = ui_utility.get_ctrl_id_by_path(
        self.banner_normal_id_, "CntGameHeadMask\\CntGameHeadIcon");
    -- 游戏状态icon
    self.state_btn_id_ = ui_utility.get_ctrl_id_by_path(self.banner_normal_id_, "CtnGameInfo\\BtnStateIcon");
    self.state_btn_ptr_ = ui_utility.get_ctrl_ptr_by_path(self.banner_normal_id_, "CtnGameInfo\\BtnStateIcon");
    -- 进度条
    self.lvl_progress_bg_ = ui_utility.get_ctrl_id_by_path(self.banner_normal_id_, "LvlProgressBarBackground");
    self.lvl_progress_ = ui_utility.get_ctrl_ptr_by_path(self.banner_normal_id_,
            "LvlProgressBarBackground\\LvlProgressBar");
    -- 下载或者更新完成的蓝点
    self.blue_point_ptr_ = ui_utility.get_ctrl_ptr_by_path(self.banner_normal_id_, "BluePoint");
    if self:is_subscription_banner() then
        local red_point_ctrl = ui_utility.get_ctrl_id_by_path(self.banner_normal_id_, "BluePoint");
        ui_utility.setproperty_by_id(red_point_ctrl
         , "Background", "ONEIMAGE:banner_list\\red_point.png;Center;null;");
    end
    ui_utility.setproperty_by_id(self.state_btn_id_,"Background",WGBannerStateIcon.UPDATE_ICON);
    -- tips红点
    self.tips_red_point_ptr_ = ui_utility.get_ctrl_ptr_by_path(
        self.banner_normal_id_, "CtnGameInfo\\CntGameTips");
    if nil ~= self.tips_red_point_ptr_ then
        self.tips_red_point_ptr_:Show(FALSE);
    end
    GameTipsMgr.add_banner_game_id(self.game_id_);
    local can_show, tips = GameTipsMgr.has_tips_to_show(self.game_id_);
    self.expand_tips_info_ = tips;
    GameTipsMgr.start_tick();
    self.version_ = "";

end

function WGBanner:log(msg, core_msg)
    base_utility.log(string.format("[WGBanner][%d] %s", self.game_id_, msg), core_msg);
end

function WGBanner.static_log(msg, core_msg)
    base_utility.log(string.format("[WGBanner] %s", msg), core_msg);
end

function WGBanner:is_subscription_banner()
   if self.type_ == WGBannerType.BANNER_TYPE_SUBSCRIPTION then
        return true;
   end
   return false;
end

-- 刷新banner数据
function WGBanner:refresh_base_data()
    --self:log(string.format("refresh_base_data"));

    -- 刷新基础数据
    local game_id = self.game_id_;
    local base_info = banner_model.get_game_base_info(game_id);

    if base_info == nil then
        self:log(string.format("base info is nil"), true);
        return;
    end;

    self:log("WGBanner:refresh_base_data, game name:" .. base_info.name);

    -- 设置游戏头像
    if base_info.banner_mini_icon_url ~= nil and base_info.banner_mini_icon_url ~= "" then
        self:set_game_head_icon(base_info.banner_mini_icon_url);
    else
        self:set_game_head_icon(base_info.banner_icon_url);
    end

    self:refresh_state();
end

function WGBanner:get_game_status()
    return self.game_status_;
end

function WGBanner:get_update_post_url()
    return self.update_post_url_;
end


-- 刷新banner状态
function WGBanner:refresh_state()
    if self.preupdate_status_show_ then
         self:log(string.format("preupdate doing, do not refresh_state"));
         return
    end

    --self:log(string.format("refresh_state"));
    -- 刷新状态数据
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");

    if nil == game_lib then
        return;
    end
    -- 获取当前游戏的游戏信息
    local game_info = banner_model.get_game_base_info(self.game_id_);
    local game_state_info = ierd_tgp.game_library.GameState:new();
    
    if game_info == nil then
        self:log("[refresh_state]game_info is nil");
        return;
    end

    if game_state_info == nil then
        self:log(string.format("[refresh_state]game_state is nil"), true);
        return;
    end;

    -- 填充数据
    if self.is_english_ver == true and game_info.e_name ~= nil and game_info.e_name ~= "" then
        self.game_name = game_info.e_name;
    else
        self.game_name = game_info.name;
    end

    game_lib:get_game_state(self.game_id_, game_state_info);

    local state_value = 0;
    if game_state_info.attach_game_state ~= nil and game_state_info.attach_game_state.iGame_id ~= 0 then
        self.select_game_id_ = game_state_info.attach_game_state.iGame_id;
        self.cur_banner_state_ = game_state_info.attach_game_state.iState;
        self.cur_banner_sub_state_ = game_state_info.attach_game_state.iSub_state;
        self.fProgress = game_state_info.attach_game_state.iProgress;
        self.errcode = game_state_info.attach_game_state.tcls_update_err_code;
        self.support_silent_install  = game_state_info.attach_game_state.support_silent_install;
        state_value = game_state_info.attach_game_state.iState;
    else
        self.select_game_id_ = self.game_id_;
        self.cur_banner_state_ = game_state_info.iState;
        self.cur_banner_sub_state_ = game_state_info.iSub_state;
        self.fProgress = game_state_info.iProgress;
        self.errcode = game_state_info.tcls_update_err_code;
        self.support_silent_install  = game_state_info.support_silent_install;
        state_value = game_state_info.iState;
    end;

    -- 只对网吧版本非单机游戏进行处理
    local channel = game_lib:get_game_distribution_channels(self.select_game_id_);
    if banner_model.is_netbar == 1 and 
        banner_model.distribution_channels.CHANNELS_RAIL ~= channel and
        state_value == ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED then
        self:log(string.format(
            "[refresh_state]change not_isntalled to normal for netbar version"));
        state_value = ierd_tgp.game_library.GAME_STATE_NORMAL;
    end
    self.game_status_ = state_value;
    self.cur_banner_state_ = state_value;
    
    local plugin_state_info = banner_model.get_plugin_state(self.select_game_id_);
    if plugin_state_info ~= nil then
        self.plugin_status = plugin_state_info.status;
    end

    self:log(string.format("[refresh_state]game state:%d, sub_state:%d, game = %d, select=%d", 
                           state_value, self.cur_banner_sub_state_, self.game_id_, self.select_game_id_));
    self:show_game_status_text(state_value); 
 
    game_state_info:delete();
    self:determine_blue_point_state(state_value);
    if self:is_subscription_banner() then
        self:determine_red_point_state();
    end

    if nil ~= self.game_text_tip_id_ and 
        Tenio.INVALID_CONTROLID ~= self.game_text_tip_id_ then
        ui_utility.setproperty_by_id(self.game_text_tip_id_, "TextTip", self.game_name);
    end

    if false == self.move_to_game_list_ and
        WGBannerType.BANNER_TYPE_SUBSCRIPTION == self.type_ and
        ierd_tgp.game_library.GAME_STATE_DOWNLOADING == state_value then
        self.move_to_game_list_ = true;
        banner_model.remove_from_subscription_list(self.game_id_);
        banner_control.toggle_my_game_list();
    end
end

function WGBanner:refresh_preupdate_state()
    local game_id = self.game_id_;
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    if nil == game_lib then
        return;
    end
    local preupdate_status_info = ierd_tgp.game_library.Pre_Update_Status_Info:new();
    game_lib:get_preupdate_status_info(game_id, preupdate_status_info);
    if preupdate_status_info == nil then return end
    
    if preupdate_status_info.attach_preupdate_status ~= nil and 
       preupdate_status_info.attach_preupdate_status.game_id ~= 0 then
        self:show_preupdate_text(preupdate_status_info.attach_preupdate_status.current_status_, 
        preupdate_status_info.attach_preupdate_status.download_progress_, preupdate_status_info.attach_preupdate_status.download_speed_);
    else
        self:show_preupdate_text(preupdate_status_info.current_status_, 
        preupdate_status_info.download_progress_, preupdate_status_info.download_speed_);
    end;
                     
end

function WGBanner:show_preupdate_text(current_status, fProcess, fSpeed)
    if current_status == ierd_tgp.game_library.kDownloading then
        self.preupdate_status_show_ = true;
        self:update_game_text_color(0);
        local status_txt = string.format("%s...", LoadStr(OD_BA_PREUPDATE_DOWNLOADING));
        self:set_game_text(status_txt);
        ui_utility.setproperty_by_id(self.game_text_id_, "TextColor", self.text_color_); 
        ui_utility.setproperty_by_id(self.game_text_id_, "Alpha", self.text_alpha_); 
        ui_utility.setproperty_by_id(self.state_btn_id_, "Visible", "false"); 
        self:show_progress_info_helper(fProcess);
    elseif current_status == ierd_tgp.game_library.kWaitting then
        self.preupdate_status_show_ = true;
        self:update_game_text_color(0);
        local status_txt = string.format("%s...", LoadStr(ID_BA_WAIT_PREUPDATE));
        self:set_game_text(status_txt);
        ui_utility.setproperty_by_id(self.game_text_id_, "TextColor", self.text_color_); 
        ui_utility.setproperty_by_id(self.game_text_id_, "Alpha", self.text_alpha_); 
        ui_utility.setproperty_by_id(self.state_btn_id_, "Visible", "false"); 
        self:show_progress_info_helper(fProcess);
    else
        self.preupdate_status_show_ = false;
    end
    
    if not self.preupdate_status_show_ then
        self:refresh_state();
    end
end

function WGBanner:update_game_text_color(state_value)
    self.text_color_ = WGTextColor.NORMAL;
    if ierd_tgp.game_library.GAME_STATE_DOWNLOADING == state_value or
        ierd_tgp.game_library.GAME_STATE_UPDATING == state_value or
        ierd_tgp.game_library.GAME_STATE_DOWNLOAD_PAUSED == state_value or
        ierd_tgp.game_library.GAME_STATE_UNINSTALLING == state_value or
        ierd_tgp.game_library.GAME_STATE_INSTALLING == state_value or
        --ierd_tgp.game_library.GAME_STATE_NEED_UPDATE == state_value or
        ierd_tgp.game_library.GAME_STATE_SERVER_UNAVAILABLE == state_value or
        self.preupdate_status_show_ then -- 高亮
            self.text_color_ = self.text_highlight_color_;
    elseif ierd_tgp.game_library.GAME_STATE_DOWNLOAD_FAIL == state_value or
        ierd_tgp.game_library.GAME_STATE_BROKEN == state_value or
        ierd_tgp.game_library.GAME_STATE_UPDATE_FAIL == state_value then -- 异常(红)
            self.text_color_ = WGTextColor.ERROR;
    elseif ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED == state_value then -- 不可用(灰)
            self.text_color_ = WGTextColor.DISABLED;
    end
end

function WGBanner:update_game_text_layout(state_value)
    if nil == self.game_info_ptr_ or
        nil == self.txt_game_text_ptr_ or
         nil == self.state_btn_ptr_ then
        return;
    end

    local parent_rect = self.game_info_ptr_:GetRectEx(FALSE,TRUE);
    local text_rect = self.txt_game_text_ptr_:GetRectEx(FALSE,TRUE);
    local icon_rect = self.state_btn_ptr_:GetRectEx(FASLE,TRUE);

    if ierd_tgp.game_library.GAME_STATE_NEED_UPDATE == state_value or 
           self:is_subscription_banner() then
        text_rect.right = icon_rect.left - 3;
        if ui_utility.get_dpi_scale() == 1.0 then
            self.txt_game_text_ptr_:MoveEx(text_rect,TRUE);
        end
    else
        text_rect.right = text_rect.left + parent_rect.right - parent_rect.left - 12;
        if ui_utility.get_dpi_scale() == 1.0 then
            self.txt_game_text_ptr_:MoveEx(text_rect,TRUE);
        end
    end
end

function WGBanner:show_and_update_state_icon(state_value, app_state)
    local ico_url = "";
    if subscription_state.kStateMaintein == state_value then
        ico_url = WGBannerStateIcon.DOWNLOAD_ICON;
        if ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED ~= app_state then
            ico_url = WGBannerStateIcon.SUBSCRIBED_ICON;
        end
    elseif subscription_state.kStatePurchasable == state_value then
        ico_url = WGBannerStateIcon.PURCHASE_ICON;
    elseif subscription_state.kStateDownload == state_value then
        ico_url = WGBannerStateIcon.DOWNLOAD_ICON;
        if ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED ~= app_state then
            ico_url = WGBannerStateIcon.SUBSCRIBED_ICON;
        end
    elseif subscription_state.kStateRelease == state_value then
        ico_url = WGBannerStateIcon.RELEASE_ICON;
    end
    if ico_url ~= "" then
        ui_utility.setproperty_by_id(self.state_btn_id_, "Visible", "true");
        ui_utility.setproperty_by_id(self.state_btn_id_, "Background", ico_url);
    else
        ui_utility.setproperty_by_id(self.state_btn_id_, "Visible", "false");
    end
end

function WGBanner:show_game_status_text(state_value)
    --self:log(string.format("[show_game_status_text]state_value:%d", state_value));
    local status_txt = "";
    ui_utility.setproperty_by_id(self.state_btn_id_, "Visible", "false");
    ui_utility.setproperty_by_id(self.lvl_progress_bg_, "Visible", "false");

    if WGBannerType.BANNER_TYPE_SUBSCRIPTION == self.type_ then
        local substate = banner_model.get_subscription_game_state(self.select_game_id_);
        self:show_and_update_state_icon(substate, state_value);
        if ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED == state_value then
            state_value = ierd_tgp.game_library.GAME_STATE_NORMAL; --use normal color & text
        end
    end

    self:update_game_text_color(state_value);
    self:update_game_text_layout(state_value);
    if ierd_tgp.game_library.GAME_STATE_UNINSTALLING == state_value then
        status_txt = LoadStr(ID_BT_GAME_UNINSTALL);
    elseif ierd_tgp.game_library.GAME_STATE_RUNNING == state_value then
        status_txt = LoadStr(ID_BA_GAMERUNNING);
    elseif ierd_tgp.game_library.GAME_STATE_SELECTING_SERVER == state_value then
        status_txt = LoadStr(ID_BA_ZONECHOOZING);
    elseif ierd_tgp.game_library.GAME_STATE_LAUNCHING == state_value then
        status_txt = LoadStr(ID_BA_GAMELOADING);   
    elseif ierd_tgp.game_library.GAME_STATE_NEED_UPDATE == state_value then
        local pause_status = 0;
        local update_progress = 0;
        if self.cur_banner_sub_state_ ~= ierd_tgp.game_library.GAME_UPDATE_DLC_NEED_UPDATE then
            pause_status, update_progress = self:get_game_update_pause_status(self.select_game_id_);
        end
        local is_switch_branch_game = banner_model.is_game_last_action_switch_branch(self.select_game_id_);
        if 0 ~= pause_status and 0 == is_switch_branch_game then
            if self.fProgress ~= 0 and self.fProgress ~= nil then
                update_progress = self.fProgress;
            end
            status_txt = LoadStr(ID_BA_UPDATE_PAUSE);
            self:set_progress_pause_state(update_progress);
        else
            status_txt = self.game_name;
            ui_utility.setproperty_by_id(self.state_btn_id_, "Visible", "true");        
        end
    elseif ierd_tgp.game_library.GAME_STATE_BROKEN == state_value then      
        status_txt = LoadStr(ID_BA_INVALIDVERION);
    elseif ierd_tgp.game_library.GAME_STATE_SERVER_UNAVAILABLE == state_value then
        status_txt = LoadStr(ID_BA_MAINTAIN);
    elseif ierd_tgp.game_library.GAME_STATE_UPDATE_FAIL == state_value then
        status_txt = LoadStr(ID_BA_UPDATEFAIL);   
    elseif ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED == state_value then
        status_txt = self.game_name;
    elseif ierd_tgp.game_library.GAME_STATE_DOWNLOADED == state_value then
        status_txt = LoadStr(ID_BA_DOWNFINISH);   
    elseif ierd_tgp.game_library.GAME_STATE_DOWNLOAD_FAIL == state_value then
        status_txt = LoadStr( ID_BA_DOWNFAIL);   
    elseif ierd_tgp.game_library.GAME_STATE_INSTALLING == state_value then  -- 安装中
        status_txt = LoadStr(ID_BT_INSTALLING);
        if self.support_silent_install then
            self:show_progress_info_helper(self.fProgress);
        else
            -- 不支持静默安装，隐藏进度条
            ui_utility.setproperty_by_id(self.lvl_progress_bg_, "Visible", "false");
        end
    elseif ierd_tgp.game_library.GAME_STATE_DOWNLOADING == state_value then  -- 下载中
        -- 设置进度文本
        status_txt = self:get_downloading_status_text(self.cur_banner_sub_state_);
        self:show_progress_info_helper(self.fProgress);
    elseif ierd_tgp.game_library.GAME_STATE_DOWNLOAD_PAUSED == state_value then
        status_txt = LoadStr(ID_BT_DOWNLOADPAUSE);
        self:set_progress_pause_state(self.fProgress); 
    elseif ierd_tgp.game_library.GAME_STATE_UPDATING == state_value then  -- 正在更新
        -- 设置进度文本
        local pause_status = 0;
        local update_progress = 0;
        pause_status, update_progress = self:get_game_update_pause_status(self.select_game_id_);
        local show_progress = self.fProgress;
        if 0 ~= pause_status and show_progress < update_progress then
            show_progress = update_progress;
        end

        status_txt = self:get_updating_status_text(self.cur_banner_sub_state_);
        self:show_progress_info_helper(show_progress);
    else
        -- 显示游戏名字
        status_txt = self.game_name;
    end;
    local show_text = "";
    if self.selected_ or self.in_hover_ then
        show_text = self.game_name;
    else
        show_text = status_txt;
    end
    self:set_game_text(show_text);

    ui_utility.setproperty_by_id(self.game_text_id_, "TextColor", self.text_color_);
    ui_utility.setproperty_by_id(self.game_text_id_, "Alpha", self.text_alpha_);
    -- 设置头像蒙层
   self:set_head_cover(state_value);
end

function WGBanner:set_head_cover(state_value)
    local head_cover_id = ui_utility.get_ctrl_id_by_path(self.banner_normal_id_, "CtnHeadCover");
    if ierd_tgp.game_library.GAME_STATE_NORMAL == state_value or
        ierd_tgp.game_library.GAME_STATE_RUNNING == state_value or
        ierd_tgp.game_library.GAME_STATE_SELECTING_SERVER == state_value or
        ierd_tgp.game_library.GAME_STATE_LAUNCHING == state_value or  
        ierd_tgp.game_library.GAME_STATE_UNKNOWN == state_value or
        ierd_tgp.game_library.GAME_STATE_NEED_UPDATE == state_value then
         ui_utility.setproperty_by_id(head_cover_id, "Visible", "false");
    else
         ui_utility.setproperty_by_id(head_cover_id, "Visible", "true"); 
    end  
end

function WGBanner:show_progress_info_helper(fProgress)
    local progress_value = fProgress;
    if progress_value == nil then
        progress_value = 0;
    end
    ui_utility.setproperty_by_id(self.lvl_progress_bg_, "Visible", "true");
    if self.lvl_progress_ ~= nil then
        self.lvl_progress_:SetPos(progress_value, TRUE);
        self.lvl_progress_:Show(TRUE);
        local image_str = U8toCUIStr("ONEIMAGE:banner_list\\banner_list.png?10,72,34,74;Stretch;null;");
        self.lvl_progress_:SetLevelTopImage(image_str);
    end
end

function WGBanner:set_progress_pause_state(fProgress)
    ui_utility.setproperty_by_id(self.lvl_progress_bg_, "Visible", "true");
    local progress_value = fProgress;
    if progress_value == nil then
        progress_value = 0;
    end
    if self.lvl_progress_ ~= nil then
        self.lvl_progress_:SetPos(progress_value, TRUE);
        self.lvl_progress_:Show(TRUE);
        local image_str = U8toCUIStr("ONEIMAGE:banner_list\\banner_list.png?10,72,34,74;Stretch;null;");
        self.lvl_progress_:SetLevelTopImage(image_str);
    end
end

function WGBanner:get_game_update_pause_status(game_id)
    self:log(string.format("[get_game_update_pause_status]game_id:%d", game_id));       
    local param = {};
    param["game_id"] = game_id;
    local data_out = comm_center.call_service("SvrGetGameUpdateStatus", param);
    local pause_status = 0;
    local update_progress = 0;
    if nil ~= data_out.pause_status then
        pause_status = data_out.pause_status;
    end
    if nil ~= data_out.update_progress then
        update_progress = data_out.update_progress;
    end

    return pause_status, update_progress;
end

function WGBanner:get_downloading_status_text(sub_state_value, fProcess)
    local status_txt = "";
    if ierd_tgp.game_library.GAME_DOWNLOAD_WAIT == sub_state_value then
        status_txt = LoadStr(ID_BT_WAITDOWNLOAD);
    else
        if fProcess == nil then
            status_txt = LoadStr(ID_BT_DOWNLOADING);
        else
            status_txt = string.format("%s %d%%", LoadStr(ID_BT_DOWNLOADING), fProcess);
        end
    end
    return status_txt;
end

function WGBanner:format_speed(download_speed)
    local speed_str = "";
    if (download_speed >= 1000 * 1024) then
        local speed = download_speed / (1024 * 1024);
        speed_str = string.format("%.1fM/s", speed);
    else
        local speed = download_speed / 1024;
        speed_str = string.format("%dKB/s", speed);
    end
    return speed_str;
end

function WGBanner:get_updating_status_text(sub_state_value, fProcess)
    local status_txt = "";
    if ierd_tgp.game_library.GAME_UPDATE_WAIT == sub_state_value or 
        ierd_tgp.game_library.GAME_UPDATE_DEPOT_WAIT == sub_state_value then
        status_txt = LoadStr(ID_BT_WAITUPDATE);
    else
        if fProcess == nil then
            status_txt = LoadStr(ID_BT_UPDATING);
        else
            status_txt = string.format("%s %d%%", LoadStr(ID_BT_UPDATING), fProcess);
        end
    end
    return status_txt;
end

-- 选中游戏banner节点
function WGBanner:selected_game(selected)
    if selected then
        if self:is_subscription_banner() then
            if true == banner_model.op_from_my_app_list then
                banner_model.op_from_my_app_list = false;
            else
                banner_control.ensure_subscription_list_show();
            end
        else
            banner_control.ensure_my_game_list_show();
        end
    end

    self.selected_ = selected;

    self:show_selected_mask(selected);

    if not selected and self.in_hover_ then
        self:log(string.format("[selected_game]Do not hide hover mask"));
    else
        self:show_hover_mask(selected);
    end

    if selected then
        if self.blue_point_ptr_ ~= nil and self.blue_point_ptr_:IsVisible() == TRUE then
            if self:is_subscription_banner() then
                banner_model.set_subscription_state_read(self.select_game_id_);
                banner_view.update_subscription_indicator();
                self.blue_point_ptr_:Show(FALSE);
            else
                WGBannerList:set_view_bule_point(self.select_game_id_, true);
                self.blue_point_ptr_:Show(FALSE);
                WGBannerList:set_showing_bule_point(self.select_game_id_, false);
            end
        end
        self.text_color_ = self.text_highlight_color_;
        self.text_alpha_ = WGTextColor.ALPHA_HOVER;
    else
        self.text_color_ = WGTextColor.NORMAL;
        self.text_alpha_ = WGTextColor.ALPHA_NORMAL;
    end
    if selected then
        self:show_game_status_text(self.cur_banner_state_);
    else
        if self.preupdate_status_show_ then
            self:refresh_preupdate_state();
        else
            self:show_game_status_text(self.cur_banner_state_);
        end
    end
end

function WGBanner:determine_blue_point_state(cur_state_value)
     -- 控制蓝点的显示
    if self.blue_point_ptr_ == nil then
        return;
    end
    local has_aready_view_blue_point = WGBannerList:get_view_bule_point(self.select_game_id_);
    -- 如果正在显示蓝点，则进行显示，防止刷新列表导致节点复用而出现问题
    if WGBannerList:get_is_showing_bule_point(self.select_game_id_) and 
        not has_aready_view_blue_point then
        self.blue_point_ptr_:Show(TRUE);
        return;
    end

    if has_aready_view_blue_point then
        self.blue_point_ptr_:Show(FALSE);
        return;
    end
    
    local prev_state = WGBannerList:get_prev_game_state(self.select_game_id_);
    local show_blue_point = false;
    
    -- attached game
    if prev_state == ierd_tgp.game_library.GAME_STATE_DOWNLOADING and
        cur_state_value == ierd_tgp.game_library.GAME_STATE_NORMAL then
        prev_state = ierd_tgp.game_library.GAME_STATE_INSTALLING;
    end

    if prev_state == ierd_tgp.game_library.GAME_STATE_INSTALLING and
         cur_state_value == ierd_tgp.game_library.GAME_STATE_NORMAL then
         self:log(string.format("[blue point] install complete, allow show blue point, id:%d", self.select_game_id_));
         if not has_aready_view_blue_point then
            show_blue_point = true;
         end
    end

    if prev_state == ierd_tgp.game_library.GAME_STATE_UPDATING and 
        cur_state_value == ierd_tgp.game_library.GAME_STATE_NORMAL then
         self:log(string.format("[blue point] update complete, allow show blue point, id:%d", self.select_game_id_));
        if not has_aready_view_blue_point then
            show_blue_point = true;
        end
    end

    WGBannerList:set_prev_game_state(self.select_game_id_, cur_state_value);

    if show_blue_point then
        self.blue_point_ptr_:Show(TRUE);
        WGBannerList:set_showing_bule_point(self.select_game_id_, true);
    else
        self.blue_point_ptr_:Show(FALSE);
        WGBannerList:set_showing_bule_point(self.select_game_id_, false);
    end
end

function WGBanner:determine_red_point_state()
    if self.blue_point_ptr_ == nil then
        return;
    end

    local show_red_point = banner_model.show_game_subscription_state_red_point(self.select_game_id_);
    if show_red_point then
        self.blue_point_ptr_:Show(TRUE);
    else
        self.blue_point_ptr_:Show(FALSE);
    end
end

function WGBanner:on_move()
    if self.mouse_leave_tips_to_banner_ then
        self:on_hover(true);
        self.mouse_leave_tips_to_banner_ = false;
    end
end

function WGBanner:on_hover(show)
    self:log(string.format("[on_hover]on_hover:%s ", tostring(show)));

    self.in_hover_ = show;

    if not self.selected_ then
        self:show_hover_mask(show);
    else
        self:log(string.format("[on_hover]do not play hover animation for selected game"));
    end     

    if self.expand_tips_info_ ~= nil then
        -- 判断是否是launcher启动的wegaeme，如果是，则不响应显示小蓝条显示
        local is_launcher = base_utility.is_type_of_launcher();
        if is_launcher == false then
            if show then
                GameTipsExpand:set_hover_on_banner(true);
                GameTipsExpand:set_tips_info(self.expand_tips_info_);
                local x_offset, y_offset = self:GetGameTipsExpandOffSet();
                GameTipsExpand:show_tips_expand(show, x_offset, y_offset);
            else
                GameTipsExpand:set_hover_on_banner(false);
                GameTipsExpand:tick_to_hide();
            end
        else
            base_utility.log("we game is launched by game, not show xiao lan tiao");
        end;

    end 

    if true == self.in_hover_ then
        banner_model.cur_hover_banner_game_id = self.game_id_;
    else
        banner_model.cur_hover_banner_game_id = 0;
    end

    if self.in_hover_ then
        self.text_alpha_ = WGTextColor.ALPHA_HOVER;
    else
        if self.selected_ then
            self.text_alpha_ = WGTextColor.ALPHA_HOVER;
        else
            self.text_alpha_ = WGTextColor.ALPHA_NORMAL;
        end
    end

    if self.in_hover_ then          -- hover时显示游戏名
        self:set_game_text(self.game_name);
        ui_utility.setproperty_by_id(self.game_text_id_, "TextColor", self.text_color_);
        ui_utility.setproperty_by_id(self.game_text_id_, "Alpha", self.text_alpha_);
        ui_utility.setproperty_by_id(self.game_text_tip_id_, 'TextTip', self.game_name);

        if self:is_subscription_banner() then
            local status = banner_model.get_subscription_game_state(self.select_game_id_);
            local tooltip = banner_model.get_subscription_tooltip(self.select_game_id_, status);
            if "" ~= tooltip then
                local text_tip = string.format("%s %s", self.game_name, tooltip);
                ui_utility.setproperty_by_id(self.game_text_tip_id_, 'TextTip', text_tip);
            end
        end

    else
        if self.preupdate_status_show_ then
            self:refresh_preupdate_state();
        else
            self:show_game_status_text(self.cur_banner_state_);
        end
    end
end

-- 获取弹出tips窗口的显示坐标(相对于mainframe)
function WGBanner:GetGameTipsExpandOffSet()
    local   nTempID = self.wrapper_id_;
    local   pTemp  = ui_utility.get_ctrl_ptr_by_id(nTempID);
    local   x_offset = 0;
    local   y_offset = 0;

    if (nil ~= pTemp) then
        local   rtTemp = pTemp:GetRectEx(TRUE,TRUE);

        x_offset = rtTemp.left + 190;
        y_offset = rtTemp.top;
    end;

    return x_offset, y_offset;
--[[
    local x_offset = 0;
    local y_offset = 0;
    local x = 0;
    local y = 0;

    local control_id = self.wrapper_id_;
    for i = 1, 6 do
        x, y = self:GetParentOffset(control_id);
        x_offset = x_offset + x;
        y_offset = y_offset + y;
        control_id = ui_utility.get_ctrl_parent_id(control_id);
    end;

    x_offset = x_offset + 190;  -- 120;
    y_offset = y_offset;        -- 25;

    return x_offset, y_offset;
]]
end

-- 获取control_id相对父节点的偏移坐标
function WGBanner:GetParentOffset(control_id)
    local rc = nil;
    local x = 0;
    local y = 0;
    local control_ptr = ui_utility.get_ctrl_ptr_by_id(control_id);
    if control_ptr then
        rc = control_ptr:GetRectEx(FALSE,TRUE);
    end
    if rc == nil then
        return x, y;
    end
    if rc.left ~= nil then
        x = rc.left;
    end
    if rc.top ~= nil then
        y = rc.top;
    end

    return x, y;
end

-- 显示/隐藏 游戏名
function WGBanner:show_game_name(is_show)   
    self:log(string.format("[show_game_name]is_show:%s", tostring(is_show)));

    if nil == self.txt_game_text_ptr_ then
        self:log(string.format("[show_game_name]self.txt_game_text_ptr is nil"));
        return;
    end

    self:show_game_tips_red_point(is_show);

    self.txt_game_text_ptr_:Show(is_show);
end

-- 显示/隐藏 tips红点
function WGBanner:show_game_tips_red_point(is_show)
    self.tips_red_point_can_show_ = is_show;
    if self.tips_red_point_ptr_ == nil then
        return;
    end

    if is_show == FALSE then
        self.tips_red_point_ptr_:Show(is_show);
        return;
    end;

    if self.expand_tips_info_ ~= nil then
        self.tips_red_point_ptr_:Show(is_show);
    end;
end

-- 设置tips信息已读
function WGBanner:set_game_tips_read()
    self.expand_tips_info_ = nil;
end

-- 更新 tips红点
function WGBanner:update_game_tips_red_point()
    local can_show, tips = GameTipsMgr.has_tips_to_show(self.game_id_);
    self.expand_tips_info_ = tips;
    
    if self.tips_red_point_ptr_ == nil then
        return;
    end

    if self.expand_tips_info_ ~= nil and 
       self.tips_red_point_can_show_ == TRUE then
        self.tips_red_point_ptr_:Show(TRUE);
    else
        self.tips_red_point_ptr_:Show(FALSE);
    end
end

function WGBanner:show_hover_mask(show)
    --self:log(string.format("[show_hover_mask]show_hover_mask:%s ", tostring(show)));

    if WGBannerList:is_list_scrollbar_show(self.type_) then
        self:on_scrollbar_show(true);
    else
        self:on_scrollbar_show(false);
    end

    local behavior_enter = ui_utility.get_ctrl_ptr_by_path(
        self.wrapper_id_, "CntHighlight\\BehaviorEnter");
    local behavior_leave = ui_utility.get_ctrl_ptr_by_path(
        self.wrapper_id_, "CntHighlight\\BehaviorLeave");

    if nil == behavior_enter or nil == behavior_leave then
        self:log(string.format(
        "[show_hover_mask], behavior_enter or behavior_leave is nil"));
        return;
    end

    if show then
        --self:log(string.format("[show_hover_mask]play enter animation for hove mask"));
        behavior_leave:Stop();
        behavior_enter:Start();
    else
        --self:log(string.format("[show_hover_mask]play leave animation for hove mask"));
        behavior_enter:Stop();
        behavior_leave:Start();
    end
end

function WGBanner:show_selected_mask(show)
    --self:log(string.format("[show_selected_mask]show_selected_mask:%s ", tostring(show)));
    ui_utility.show_child(self.wrapper_id_, "CntHighlight\\CntSelectedMask", show);
end

-- 设置游戏名
function WGBanner:set_game_text(name_state_text)
    --self:log(string.format("set_game_text, %s", name_state_text));

    if nil == self.txt_game_text_ptr_ then
        self:log("set_game_name, txt_game_text_ptr_ is nil");
        return;
    end
  
    local cui_str = U8toCUIStr(name_state_text);
    if cui_str ~= nil then
        self.txt_game_text_ptr_:SetText(cui_str);
    end
end

function WGBanner:get_cut_str(in_str)
    local is_need_cut_name = false;
    local max_len = self.MAX_DISPALY_CHAR_COUNT;

    local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
    local is_english_ver = tpfui:Is_English_ver();
    
    if true == is_english_ver then
        max_len = self.MAX_EN_DISPALY_CHAR_COUNT;
    end

    is_need_cut_name = ui_utility.is_over_char_count(in_str, max_len);
    local out_str = in_str;
    if true == is_need_cut_name then
        out_str = U8toCUIStrn(in_str,
           max_len - 1, 1);
    else
        out_str = U8toCUIStr(in_str);
    end
    return is_need_cut_name, out_str;
end

-- 设置游戏头像
function WGBanner:set_game_head_icon(url)
    --self:log(string.format("set_game_head_icon, %s", url));

    if self.game_head_id_ ~= nil and self.game_head_id_ ~= Tenio.INVALID_CONTROLID then
        local pro_value = string.format(
        "ONEIMAGE:banner_list\\banner_list.png?78,10,102,34|%s;Stretch;null;", url);
        ui_utility.setproperty_by_id(self.game_head_id_, "Background", pro_value);
    end;
end

function WGBanner:deal_long_game_name(game_name)
    local is_need_cut_name = false;
    if game_name ~= nil then
        is_need_cut_name = ui_utility.is_over_char_count(game_name, self.MAX_DISPALY_CHAR_COUNT);
        if is_need_cut_name == true then --超过5个字符，暂时处理 TODO
            ui_utility.setproperty_by_id(self.game_text_id_, "AutoSize", "false");
            ui_utility.setproperty_by_id(self.game_text_id_, "Width", "73");
        end
    end
end

function WGBanner:get_is_in_hover()
    return self.in_hover_;
end

-- Banner上面GameTips在显示与隐藏时候的布局变化
function WGBanner:game_tips_visible_change(is_show, click_to_close)
    --self:log(string.format("[game_tips_visible_change]is_show:%s", tostring(is_show)));

    if TRUE == is_show or true == is_show then
        if ierd_tgp.game_library.GAME_STATE_NORMAL == self.cur_banner_state_ then
            ui_utility.setproperty_by_id(self.wrapper_id_,
                "Height", WGBanner.BANNER_WRAPPER_HEIGHT + WGBanner.TIPS_HEIGHT);
             WGBannerList:re_layout_item_pos();
        end
    else
        ui_utility.setproperty_by_id(self.wrapper_id_,
            "Height", WGBanner.BANNER_WRAPPER_HEIGHT);
        WGBannerList:re_layout_item_pos();
    end
end


function WGBanner:excute_game_act(game_action)
    self:log(string.format("excute_game_act, action:%d", game_action));

    local action_info = {
        game_id_ = self.game_id_,
        zone_id_ = self.selected_zone_id_,
        version_ = self.version_,
        uid_ = 0,
        launch_op_from = ierd_tgp.game_library.LAUNCH_OP_FROM_MAIN_WND
    };

    WGGameAction.excute_action(game_action, action_info);
end

-- 判断是否tips的子孙节点
function WGBanner:is_banner_tips_off_spring(control_id)
    if self.banner_tips_ptr_ == nil  or 
       control_id == nil then
        return false;
    end
    if self.banner_tips_ptr_:IsMyOffspring(control_id) == 1 then
        return true;
    else
        return false;
    end
end

-- 鼠标是否从tips离开进入banner区域
function WGBanner:leave_tips_to_banner(ret)
      self.mouse_leave_tips_to_banner_ = ret;
end

-- 获取错误码
function WGBanner:get_err_code()
    return self.errcode;
end

function WGBanner:banner_tips(banner_id)
    -- 新增二次确认框，下载中游戏删除 Modified by Rduolfzhang 20161019
    -- （想放在popup_wnd中统一，但由于功能较小，统一起来改动的文件较多，因此决定还是直接放这里）
    -- （并且将来banner可能还需要其他弹框，逻辑可以复用）
    -- 只有大型网游才弹出确认框
    local banner = WGBannerList:get_banner_by_ctrl(banner_id);
    local main_id = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame);
    if banner == nil then
        base_utility.log(
        string.format("[WGBanner][banner_tips] banner is NIL, ctrl_id:%d",
        banner_id), true);
        return;
    end
    local tipParam = {
        position = "center",
        title = LoadStr("WE_GAME_BANNER_BANNER_TIPS"),
        width = 480,
        height = 230,
        titleIcon = "",
        icon = "",
        tipsIcon = poptips_icons.kError,
        descTitle = "",
        desc = LoadStr("WE_GAME_BANNER_BANNER_REMOVE"),
        parent_id = main_id,
        modal = false,
        always_center = true,
        okBtn = {
            visible = "true",
            text = LoadStr("WE_GAME_BANNER_BANNER_OK"),
            close_win = true,
            handle = function()
                banner:excute_game_act(GameActions.kCancelDownload);
            end
        },
        cancelBtn = {
            visible = "true",
            text = LoadStr("WE_GAME_BANNER_BANNER_CANCEL"),
            handle = function()
            end
        }
    }
    poptips_control.show_tips(tipParam);
    return true;
end

function WGBanner:on_scrollbar_show(show)
    if nil == self.cnt_highlight_ctrl then
        self.cnt_highlight_ctrl = ui_utility.get_ctrl_id_by_path(self.wrapper_id_, "CntHighlight");
    end

    if nil == self.wrapper_width then
        local ww, wh = ui_utility.get_control_size_by_id(self.wrapper_id_);
        if ww > 170 then
            self.wrapper_width = ww;
        end
    end

    if nil == self.wrapper_width then
        return;
    end

    if show then
        ui_utility.setproperty_by_id(self.cnt_highlight_ctrl, "Width", 
                                                            tostring(self.wrapper_width - 9));
    else
        ui_utility.setproperty_by_id(self.cnt_highlight_ctrl, "Width",
                                                            tostring(self.wrapper_width));
    end
end


-- 以.链接的函数都是界面响应函数，全局性质
------------------------------WGBannerUIEventListener------------------------------------------------


function WGBannerUIEventListener.on_banner_mouse_hover(uilib, control_id, x, y, id_message_from)
    base_utility.log(string.format("[WGBannerUIEventListener]on_banner_mouse_hover ctrl_id:%d",
    control_id));
    local banner_id = 0;
    banner_id = ui_utility.get_ctrl_parent_id(control_id);

    local banner = WGBannerList:get_banner_by_wrapper(banner_id);
    if banner == nil then
        base_utility.log(string.format("[WGBannerUIEventListener][on_banner_mouse_hover]banner is NIL," ..
        " ctrl_id:%d", control_id), true);
        return;
    end

    banner:on_hover(true);
end

function WGBannerUIEventListener.on_banner_mouse_leave(uilib, control_id, x, y, id_message_from)
    base_utility.log(string.format(
    "[WGBannerUIEventListener]on_banner_mouse_leave ctrl_id:%d", control_id));

    local banner_id = 0;
    banner_id = ui_utility.get_ctrl_parent_id(control_id);

    local banner = WGBannerList:get_banner_by_wrapper(banner_id);
    if banner == nil then
        base_utility.log(string.format(
        "[WGBannerUIEventListener][on_banner_mouse_leave]banner is NIL," ..
        " ctrl_id:%d", control_id), true);
        return;
    end

    banner:on_hover(false);
end


function WGBannerUIEventListener.on_banner_click(uilib, control_id, x, y)
    base_utility.log(string.format("[WGBannerUIEventListener]on_banner_click ctrl_id:%d", control_id));

    local banner_id = ui_utility.get_ctrl_parent_id(control_id);      

    local banner = WGBannerList:get_banner_by_ctrl(banner_id);

    if banner == nil then
        base_utility.log(string.format(
        "[WGBannerUIEventListener][on_banner_click] banner is NIL, ctrl_id:%d",
        control_id), true);
        return;
    end

    local param = { };
    param.module_id = banner.game_id_;
    param.check_attach = 1;  -- 主动点击banner节点需要打开上次记录助手页
    param.ctrl_id = control_id;
    event_center.send_event(banner_event.JUMP_MODULE_WEB, base_utility.table_to_json(param));
end

function WGBannerUIEventListener.on_banner_dbclick(uilib, control_id, x, y)
    banner_control.banner_report(banner_qos_action.kActionBannerDbClick);
end

function WGBannerUIEventListener.on_game_list_mouse_wheel(uilib, control_id)
    event_center.send_event(banner_menu_event.HIDE_GAME_MENUE, "");
    if game_mgr_menu ~= nil then
        game_mgr_menu.hide();
    end                                       
end

function WGBannerUIEventListener.on_banner_right_btn_down(uilib, control_id, x, y)

    local banner_id = ui_utility.get_ctrl_parent_id(control_id);      
    local banner = WGBannerList:get_banner_by_ctrl(banner_id);
    if banner == nil then
        base_utility.log(string.format(
            "[WGBannerUIEventListener][on_banner_right_btn_down] banner is NIL, ctrl_id:%d",
                control_id), true);
        return;
    end

    local rc = nil;
    local control_ptr = ui_utility.get_ctrl_ptr_by_id(control_id);
    if control_ptr ~= nil then
        rc = control_ptr:GetRectEx(TRUE,TRUE);
    end
    local tbl_param = {};
    if rc ~= nil then
        tbl_param = { xoffset = rc.left + x, yoffset = rc.top + y, id = banner.game_id_, _type = banner.type_ };
    end
    local json_str = base_utility.table_to_json(tbl_param);
    event_center.send_event(banner_menu_event.OPEN_GAME_MENUE, json_str);
end